package com.xxcxcxcx.jobsystem.mixin.job.Brewmaster;

import com.xxcxcxcx.jobsystem.Main;
import com.xxcxcxcx.jobsystem.common.Job.Brewmaster.Brewmaster;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static com.xxcxcxcx.jobsystem.common.Job.Brewmaster.Brewmaster.*;

@Mixin(Arrow.class)
public class ArrowMixin {

    @Unique private boolean hasPotionBonus;
    @Unique private int level=0;
    @Unique private float time=0;

    @Inject(method = "setEffectsFromItem", at = @At("TAIL"))
    private void originsClasses$initFromAdditionalPotionNbt(ItemStack arrow, CallbackInfo ci) {
        this.hasPotionBonus = Brewmaster.hasPotionBonus(arrow);
        if(hasPotionBonus){
            level = arrow.getOrCreateTagElement(Main.MOD_ID).getInt(Refining+RefiningLevel);
            time = arrow.getOrCreateTagElement(Main.MOD_ID).getFloat(Refining+Refiningtime);
        }
    }

    @ModifyArg(method = "doPostHurtEffects", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;addEffect(Lnet/minecraft/world/effect/MobEffectInstance;Lnet/minecraft/world/entity/Entity;)Z"), index = 0)
    private MobEffectInstance originsClasses$handlePotionBonus(MobEffectInstance effect) {
        return hasPotionBonus ? Brewmaster.applyPotionBonus(effect,level,time) : effect;
    }

}
